package com.itstorm.pomeloweather.ui;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.itstorm.pomeloweather.R;
import com.itstorm.pomeloweather.util.BroadcastCode;

public class MenuFragment extends Fragment {

	private TextView tvAdd;
	private TextView tvSetting;
	private TextView tvAbout;
	private TextView tvEdit;
	private RadioGroup radioGroup;
	private BroadcastReceiver mReceiver;
	private SharedPreferences weatherList;
	private String[] mAreas;
	private View view;

	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		inflater = LayoutInflater.from(getActivity());
		view = inflater.inflate(R.layout.activity_menu, null);

		weatherList = MenuFragment.this.getActivity().getSharedPreferences(
				"WeatherList", 0);
		mAreas = weatherList.getString("Areas", "").split(",");

		tvAdd = (TextView) view.findViewById(R.id.tv_add);
		tvSetting = (TextView) view.findViewById(R.id.tv_setting);
		tvAbout = (TextView) view.findViewById(R.id.tv_about);
		tvEdit = (TextView) view.findViewById(R.id.tv_edit);
		radioGroup = (RadioGroup) view.findViewById(R.id.radioGroup);
		for (int i = 0; i < mAreas.length; i++) {
			initChild(mAreas[i], i);
		}
		radioGroup.getChildAt(0).setSelected(true);
		tvAdd.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent mIntent = new Intent(getActivity(),
						AddAreasActivity.class);
				startActivity(mIntent);
				getActivity().overridePendingTransition(R.anim.alpha_action20,
						R.anim.alpha_action);
			}
		});
		tvEdit.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent mIntent = new Intent(getActivity(),
						EditAreasActivity.class);
				startActivity(mIntent);
				getActivity().overridePendingTransition(R.anim.alpha_action20,
						R.anim.alpha_action);
			}
		});
		tvSetting.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent mIntent = new Intent(getActivity(),
						SettingActivity.class);
				startActivity(mIntent);
				getActivity().overridePendingTransition(R.anim.alpha_action20,
						R.anim.alpha_action);
			}
		});
		tvAbout.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent();
				intent.setClass(getActivity(), AboutActivity.class);
				startActivity(intent);
				getActivity().overridePendingTransition(R.anim.alpha_action20,
						R.anim.alpha_action);
			}
		});

		mReceiver = new BroadcastReceiver() {

			@Override
			public void onReceive(Context context, Intent intent) {
				// TODO Auto-generated method stub
				if (intent.getAction().equals(BroadcastCode.changeAreas)) {

					Bundle extras = intent.getExtras();
					int type = extras.getInt("type", 0);
					String areas = extras.getString("areas", "");
					switch (type) {
					case BroadcastCode.addArea:
						weatherList = MenuFragment.this.getActivity()
								.getSharedPreferences("WeatherList", 0);
						mAreas = weatherList.getString("Areas", "").split(",");

						radioGroup.removeAllViews();
						for (int i = 0; i < mAreas.length; i++) {
							initChild(mAreas[i], i);
						}
						radioGroup.getChildAt(mAreas.length - 1).setSelected(
								true);
						break;
					case BroadcastCode.delArea:

						weatherList = MenuFragment.this.getActivity()
								.getSharedPreferences("WeatherList", 0);
						mAreas = weatherList.getString("Areas", "").split(",");
						radioGroup.removeAllViews();
						for (int i = 0; i < mAreas.length; i++) {
							initChild(mAreas[i], i);
						}
						radioGroup.getChildAt(mAreas.length - 1).setSelected(
								true);
						break;
					case BroadcastCode.changeArea:

						weatherList = MenuFragment.this.getActivity()
								.getSharedPreferences("WeatherList", 0);
						mAreas = weatherList.getString("Areas", "").split(",");
						if (!BaseActivity.slidingMenu.isMenuShowing()) {
							int position = extras.getInt("position", 0);
							radioGroup.removeAllViews();
							for (int i = 0; i < mAreas.length; i++) {
								initChild(mAreas[i], i);
							}
							radioGroup.getChildAt(position).setSelected(true);
						}
						break;
					default:
						break;
					}
				}
			}
		};
		IntentFilter mIntentFilter = new IntentFilter(BroadcastCode.changeAreas);
		MenuFragment.this.getActivity().registerReceiver(mReceiver,
				mIntentFilter);

		return view;
	}

	public void initChild(String areasName, final int position) {
		RadioButton tempButton = new RadioButton(
				MenuFragment.this.getActivity());
		tempButton.setButtonDrawable(R.drawable.selector_areas_item);
		tempButton.setPadding(25, 0, 0, 0);
		tempButton.setText(areasName);
		tempButton.setTextColor(0xb0ffffff);
		tempButton.setTextSize(20f);
		tempButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				weatherList = MenuFragment.this.getActivity()
						.getSharedPreferences("WeatherList", 0);
				mAreas = weatherList.getString("Areas", "").split(",");

				for (int i = 0; i < mAreas.length; i++) {
					if (i != position) {
						radioGroup.getChildAt(i).setSelected(false);
					} else {
						radioGroup.getChildAt(i).setSelected(true);
					}
				}
				Intent mIntent = new Intent();
				mIntent.setAction(BroadcastCode.changeAreas);
				mIntent.putExtra("type", BroadcastCode.changeArea);
				mIntent.putExtra("position", position);
				MenuFragment.this.getActivity().sendBroadcast(mIntent);
				BaseActivity.slidingMenu.showContent();
			}
		});

		radioGroup.addView(tempButton, position, new LayoutParams(
				LinearLayout.LayoutParams.FILL_PARENT,
				LinearLayout.LayoutParams.WRAP_CONTENT));
	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		MenuFragment.this.getActivity().unregisterReceiver(mReceiver);
	}

}
